package com.lovo.common.frame;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.lovo.active.frame.ActivePanel;
import com.lovo.classinfo.ClassPanel;
import com.lovo.dept.frame.DeptPanel;
import com.lovo.emp.frame.EmpPanel;
import com.lovo.netCRM.component.LovoTree;
import com.lovo.netCRM.component.LovoTreeNode;
import com.lovo.school.frame.SchoolPanel;
import com.lovo.student.frame.StudentPanel;
import com.lovo.system.frame.LogPanel;
import com.lovo.system.frame.UpdatePassPanel;
import com.lovo.total.frame.AreaTotalPanel;
import com.lovo.total.frame.SchoolTotalPanel;
import com.lovo.work.frame.WorkPanel;


/**
 * 
 * CRM系统
 * @description 布局主界面
 */
public class MainFrame extends JFrame{
	/**树形菜单*/
	private LovoTree tree;
	/**右边的容器组件*/
	private JPanel mainPanel = new JPanel();
	/**卡片布局*/
	private CardLayout card = new CardLayout();

	/**员工主面板*/
	private EmpPanel empPanel = new EmpPanel(this);
	/**部门主面板*/
	private DeptPanel deptPanel = new DeptPanel(this);
	/**职位主面板*/
	private WorkPanel workPanel = new WorkPanel(this);
	/**学校活动主面板*/
	private ActivePanel activePanel = new ActivePanel(this);
	/**学校主面板*/
	private SchoolPanel schoolPanel = new SchoolPanel(this);
	/**学生主面板*/
	private StudentPanel studentPanel = new StudentPanel(this);
	/**班级主面板*/
	private ClassPanel classPanel = new ClassPanel(this);
	/**地区统计主面板*/
	private AreaTotalPanel areaTotalPanel = new AreaTotalPanel(this);
	/**学校统计主面板*/
	private SchoolTotalPanel schoolTotalPanel = new SchoolTotalPanel(this);
	/**日期一览主面板*/
	private LogPanel logPanel = new LogPanel(this);
//	/**修改密码面板*/
	private UpdatePassPanel updatePassPanel;
	/**登陆用户*/
	private Object loginUser;
			
	public MainFrame(Object loginUser){
		super("CRM系统");
		this.loginUser = loginUser;
		
		this.initTree();
		this.initPanel();
		
		
		this.setSize(1000,700);
		this.setVisible(true);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
	}
	/**
	 * 初始化上面的容器
	 *
	 */
	private void initPanel() {
		//设置容器的布局管理器为卡片布局
		mainPanel.setLayout(this.card);
		this.add(mainPanel);
		
		updatePassPanel = new UpdatePassPanel(loginUser, this);
		
		//给卡布局的容器添加面板，每加上一个面板，由第二个参数给面板取名
		mainPanel.add(new InitPanel(),"init");
		mainPanel.add(empPanel,"emp");
		mainPanel.add(deptPanel,"dept");
		mainPanel.add(workPanel,"work");
		mainPanel.add(activePanel,"active");
		mainPanel.add(schoolPanel,"school");
		mainPanel.add(studentPanel,"student");
		mainPanel.add(classPanel,"class");
		mainPanel.add(areaTotalPanel,"areaTotal");
		mainPanel.add(schoolTotalPanel,"schoolTotal");
		mainPanel.add(logPanel,"log");
		mainPanel.add(updatePassPanel,"updatePass");
	}
	
	
	/**
	 * 设置树形菜单
	 *
	 */
	private void initTree() {
		//根节点
		LovoTreeNode rootNode = new LovoTreeNode("四川网脉CRM系统");
		
		//枝节点
		LovoTreeNode dataNode = new LovoTreeNode("资料管理");
		LovoTreeNode schoolNode = new LovoTreeNode("学校管理");
		LovoTreeNode userNode = new LovoTreeNode("用户管理");
		LovoTreeNode totalNode = new LovoTreeNode("统计信息");
		LovoTreeNode systemNode = new LovoTreeNode("系统管理");
		
		//叶节点
		LovoTreeNode empNode = new LovoTreeNode("员工管理"){
			
			public void addListener(){
				//切换到"emp"标签指定的面板
				card.show(mainPanel, "emp");
			}
		};
		LovoTreeNode deptNode = new LovoTreeNode("部门管理"){
					
			public void addListener(){
				//切换到"dept"标签指定的面板
				card.show(mainPanel, "dept");
			}
		};
		LovoTreeNode workNode = new LovoTreeNode("职位管理"){
					
			public void addListener(){
				//切换到"work"标签指定的面板
				card.show(mainPanel, "work");
			}
		};
		LovoTreeNode activeNode = new LovoTreeNode("学校活动"){
			
			public void addListener(){
				//切换到"active"标签指定的面板
				card.show(mainPanel, "active");
			}
		};
		LovoTreeNode schoolSubNode = new LovoTreeNode("学校管理"){
			
			public void addListener(){
				//切换到"school"标签指定的面板
				card.show(mainPanel, "school");
			}
		};
		LovoTreeNode studentNode = new LovoTreeNode("学生管理"){
			
			public void addListener(){
				//切换到"student"标签指定的面板
				card.show(mainPanel, "student");
			}
		};
		LovoTreeNode classNode = new LovoTreeNode("班级管理"){
			
			public void addListener(){
				//切换到"class"标签指定的面板
				card.show(mainPanel, "class");
			}
		};
		LovoTreeNode areaTotalNode = new LovoTreeNode("地区统计"){
			
			public void addListener(){
				//切换到"areaTotal"标签指定的面板
				card.show(mainPanel, "areaTotal");
				areaTotalPanel.initData();
			}
		};		
		LovoTreeNode schoolTotalNode = new LovoTreeNode("学校统计"){
			
			public void addListener(){
				//切换到"schoolTotal"标签指定的面板
				card.show(mainPanel, "schoolTotal");
			}
		};
		LovoTreeNode updatePassNode = new LovoTreeNode("修改密码"){
			
			public void addListener(){
				//切换到"updatePass"标签指定的面板
				card.show(mainPanel, "updatePass");
			}
		};
		LovoTreeNode logNode = new LovoTreeNode("日志一览"){
			
			public void addListener(){
				//切换到"work"标签指定的面板
				card.show(mainPanel, "log");
				// 渲染表格
				logPanel.initData();

			}
		};
//		//将叶节点加入枝节点
		dataNode.add(empNode);
		dataNode.add(deptNode);
		dataNode.add(workNode);

		schoolNode.add(activeNode);
		schoolNode.add(schoolSubNode);

		userNode.add(studentNode);
		userNode.add(classNode);

		totalNode.add(areaTotalNode);
		totalNode.add(schoolTotalNode);
		
		systemNode.add(updatePassNode);
		systemNode.add(logNode);
		
		initNode(rootNode,dataNode,schoolNode,userNode,totalNode,systemNode);
		
		//创建树形菜单
		this.tree = new LovoTree(rootNode);
		//设置树形菜单的字体
		this.tree.setFont(new Font("宋体",Font.BOLD,16));
		
		//将树形菜单加入窗体
		this.add(this.tree,BorderLayout.WEST);
	}
	
	//--------------------
	/**
	 * 将枝节点加入根节点
	 */
	private void initNode(LovoTreeNode rootNode,LovoTreeNode dataNode,
			LovoTreeNode schoolNode,LovoTreeNode userNode,LovoTreeNode totalNode,LovoTreeNode systemNode){
//		UserBean user = (UserBean )this.loginUser;
//
//		if("班主任".equals(user.getGrade()) || "超级管理员".equals(user.getGrade())) {
			//加入资料管理节点
			rootNode.add(dataNode);
//		}
		//加入学校管理节点
		rootNode.add(schoolNode);
		//加入用户管理节点
		rootNode.add(userNode);
		//加入统计信息节点
		rootNode.add(totalNode);
		//加入系统管理节点
		rootNode.add(systemNode);
		
	}

	public Object getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(Object loginUser) {
		this.loginUser = loginUser;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame frame = new MainFrame(null);
	}

}
